<template>
    <div>
        <h1>test1-机试题</h1>

        <div class="group">
            <h3>1.计数器 - 测试使用 ref 来处理单个数据的响应式更新。</h3>
            <div class="counter">
                <h2>Counter: {{ counter }}</h2>
                <button @click="getIncrement">Increment</button>
                <button @click="getDecrement">Decrement</button>
            </div>
        </div>

        <div class="group">
            <h3>2.表单输入展示 - 测试使用 reactive 来管理多个表单字段的数据绑定和响应式展示。</h3>
            <div class="form-container">
                <h2>Enter your information</h2>
                <label for="name">Name:</label>
                <input type="text" id="name" v-model="user.name">

                <label for="age">Age:</label>
                <input type="number" id="age" v-model="user.age">

                <p>Your name is __{{ user.name }}___ and you are __{{ user.age }}__ years old.</p>
            </div>
        </div>

        <div class="group">
            <h3>3.待办事项列表 - 测试使用 reactive 数组来处理列表项的添加与移除操作。</h3>
            <div class="todo-list">
                <h2>Todo List</h2>
                <!-- 输入框用于输入新的待办事项 -->
                <input placeholder="Add a new todo" v-model="inpVal">
                <!-- 按钮用于添加新的待办事项 -->
                <button @click="addItem">Add</button>

                <ul>
                    <!-- 遍历待办事项数组并显示每个事项及其删除按钮 -->
                    <li v-for="(item, index) in arr" :key="index">
                        {{ item.name }}
                        <button @click="removeItem(item.id)">Delete</button>
                    </li>

                </ul>
            </div>
        </div>

        <div class="group">
            <h3>4.下面是一个购物车的静态 HTML 模板和样式部分。你可以在此基础上实现购物车的功能，包括 ref、reactive、计算属性、样式绑定等 Vue 3 知识点</h3>
            <div class="shopping-cart">
                <h1>Shopping Cart</h1>
                <button @click="getAddShop">模拟新增商品列表</button>
                <!-- 商品列表 -->
                <div class="product-list" v-for="item in shopList" :key="item.id">
                    <div class="product">
                        <img :src="item.shopImgUrl" alt="Product Image">
                        <div class="product-details">
                            <h2>{{ item.shopName }}</h2>
                            <p>$ {{ item.price }}.00</p>
                            <div class="quantity">
                                <button :disabled="item.count <= 1" @click="item.count--">-</button>
                                <input type="text" v-model="item.count"
                                    @change="() => { item.count = item.count <= 1 ? 1 : item.count }">
                                <button :disabled="item.count >= item.stock" @click="item.count++">+</button>
                            </div>
                        </div>
                    </div>

                    <!-- <div class="product">
                        <img src="https://via.placeholder.com/100" alt="Product Image">
                        <div class="product-details">
                            <h2>Product 2</h2>
                            <p>$50.00</p>
                            <div class="quantity">
                                <button>-</button>
                                <input type="text" value="1">
                                <button>+</button>
                            </div>
                        </div>
                    </div> -->
                </div>

                <!-- 购物车总结 -->
                <div class="cart-summary">
                    <h3>Cart Summary</h3>
                    <p>Total Items: <span>{{ shopList.length }}</span></p>
                    <p>Total Price: <span>$ {{ getTotalPrice }}.00</span></p>
                    <button class="checkout-btn">Checkout</button>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup lang='ts'>

import { computed, reactive, ref } from 'vue';
//1.
const counter = ref(0)
const getIncrement = () => {
    counter.value++;
}
const getDecrement = () => {
    counter.value--;
}

//2.
const user = reactive({
    name: "lwj",
    age: 22
})

// 3.
// 初始化 reactive 数组用于储存代办事项
const arr = reactive([
    { id: 1, name: 'Sample Todo 1' },
    { id: 2, name: 'Sample Todo 2' },
    { id: 3, name: 'Sample Todo 3' }
])

// 绑定输入框得值
const inpVal = ref('')

// 记录下一个待办事项得ID
let nextId = arr.length + 1;

// 添加新的待办事项到数组中
const addItem = () => {
    if (inpVal.value.trim() !== '') {
        arr.push({
            id: nextId++,
            name: inpVal.value
        });
    }
}

// 根据 id 从待办事项中移除某一项
const removeItem = (id: number) => {
    const index = arr.findIndex(item => item.id === id);
    if (index !== -1) {
        arr.splice(index, 1);
    }
}

// 4.
interface IShopInfo {
    id: string
    shopName: string
    count: number
    price: number
    stock: number
    shopImgUrl: string
}

const shopList = ref<IShopInfo[]>([
    {
        id: '1',
        shopName: 'Prodect 1',
        count: 1,
        price: 100,
        stock: 10,
        shopImgUrl: '/src/assets/images/5.jpg'
    },
    {
        id: '2',
        shopName: 'Prodect 2',
        count: 1,
        price: 50,
        stock: 5,
        shopImgUrl: '/src/assets/images/6.png'
    },
    {
        id: '3',
        shopName: 'Prodect 3',
        count: 1,
        price: 200,
        stock: 7,
        shopImgUrl: '/src/assets/images/7.png'
    },
])

const getTotalPrice = computed(() => {
    return shopList.value.reduce((pre, cur) => { return pre + cur.count * cur.price }, 0)
})

const getAddShop = () => {
    return shopList.value.push({
        id: '3',
        shopName: 'Prodect 3',
        count: 1,
        price: 200,
        stock: 7,
        shopImgUrl: '/src/assets/images/8.jpg'
    },)
}
</script>

<style lang="scss" scoped>
.group {
    border: 2px solid skyblue;
    padding: 10px;
    margin: 10px;
}

//1.

.counter {
    text-align: center;
}

button {
    margin: 5px;
    padding: 10px;
    background-color: #4caf50;
    color: white;
    border: none;
    cursor: pointer;
}

button:hover {
    background-color: #45a049;
}

// 2.
.form-container {
    margin: 20px;
}

input {
    display: block;
    margin: 10px 0;
    padding: 5px;
}

label {
    font-weight: bold;
}

p {
    margin-top: 20px;
    font-size: 1.2em;
}



// 3.
.todo-list {
    text-align: center;
}

input {
    padding: 5px;
    margin-right: 10px;
}

button {
    padding: 5px;
    background-color: #2196f3;
    color: white;
    border: none;
    cursor: pointer;
}

button:hover {
    background-color: #0b7dda;
}

ul {
    list-style: none;
    padding: 0;
}

li {
    cursor: pointer;
    padding: 10px;
    margin: 5px 0;
    background-color: #f1f1f1;
}

li:hover {
    background-color: #ddd;
}

// 4.
/* 主体购物车样式 */
.shopping-cart {
    width: 80%;
    margin: 20px auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 10px;
    background-color: #f9f9f9;
}

/* 商品列表 */
.product-list {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.product {
    display: flex;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid #ddd;
}

.product img {
    width: 100px;
    height: 100px;
    margin-right: 20px;
}

.product-details {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.product-details h2 {
    margin: 0;
}

.product-details p {
    font-size: 1.2em;
    font-weight: bold;
    color: #333;
}

/* 数量调节按钮 */
.quantity {
    display: flex;
    align-items: center;
}

.quantity button {
    padding: 5px 10px;
    margin: 0 5px;
    background-color: #4caf50;
    color: white;
    border: none;
    cursor: pointer;
}

.quantity button:hover {
    background-color: #45a049;
}

.quantity input {
    width: 40px;
    text-align: center;
}

/* 购物车总结 */
.cart-summary {
    margin-top: 20px;
    padding-top: 20px;
    border-top: 2px solid #ccc;
    text-align: right;
}

.cart-summary h3 {
    margin-bottom: 10px;
}

.cart-summary p {
    font-size: 1.2em;
    margin: 5px 0;
}

.checkout-btn {
    padding: 10px 20px;
    background-color: #2196f3;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 1em;
}

.checkout-btn:hover {
    background-color: #0b7dda;
}

button:disabled,
button:disabled:hover {
    background-color: #ddd;
}
</style>